Skip to content

Add typed Template identifier and support for v2 templates (ima-ngv2/ima-sigv2/ima-modsig/evm-sig)#5

Merged
hyperfinitism merged 1 commit into
mainfrom
codex/refactor-ima-log-structure-to-use-enums
Apr 29, 2026
Merged

Add typed Template identifier and support for v2 templates (ima-ngv2/ima-sigv2/ima-modsig/evm-sig)#5
hyperfinitism merged 1 commit into
mainfrom
codex/refactor-ima-log-structure-to-use-enums

Conversation

@hyperfinitism
Copy link
Copy Markdown
Owner

@hyperfinitism hyperfinitism commented Apr 29, 2026

Motivation

  • Replace the plain template_name: String with a typed Template identifier to centralize recognition of built-in templates and enable parsing/decoding of newer template variants.
  • Add first-class support for newer/wider IMA template types (ima-ngv2, ima-sigv2, ima-modsig, evm-sig) and typed digest namespaces (d-ngv2) so the library can round-trip and hash these templates correctly.

Description

  • Introduce Template enum and replace Event::template_name: String with Event::template: Template, adding Template::as_str() and Display impls; update docs and examples to use template.as_str().
  • Add DigestType and DigestV2 types and parsing/encoding helpers (parse_prefixed_digest_v2, decode_d_ngv2, encode_d_ngv2) to support d-ngv2 style prefixed digests.
  • Extend TemplateData with variants for ImaModsig, ImaNgV2, ImaSigV2, and EvmSig and implement corresponding ASCII and binary parsing/decoding functions (parse_ima_modsig, parse_ima_ngv2, parse_ima_sigv2, parse_evm_sig, and decode_ima_modsig, decode_ima_ngv2, decode_ima_sigv2, decode_evm_sig).
  • Update binary parser and ASCII parser call sites to use Template::parse and switch matching from string names to the new Template variants; update template-hash framing/encoding (collect_fields, feed_event_into, legacy handling) to include the new templates and d-ngv2 encoding.
  • Add small helpers parse_u32_le and parse_u16_le used by evm-sig decoding and introduce TemplateFieldId enum for future field id usage.
  • Update examples and tests to use the new Template API and to expect the new template variants and behaviors.

Testing

  • Ran unit tests under src/log and updated inline tests (parse_ascii_line, binary parser tests) which now assert against event.template.as_str() and new variants; those tests were updated to pass.
  • Ran integration tests in tests/ (including ascii_and_binary_agree, binary_parser_yields_multiple_events, and template-hash verification tests) after adjusting expectations to the Template API; automated tests succeeded.
  • Examples compiled and updated example output paths to use template.as_str(); example builds completed during the test run.

@hyperfinitism hyperfinitism self-assigned this Apr 29, 2026
@hyperfinitism hyperfinitism added the enhancement New feature or request label Apr 29, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors IMA template handling by replacing string-based template names with a structured Template enum and adding support for several new template types, including ima-ngv2, ima-sigv2, ima-modsig, and evm-sig. The changes include new data structures for v2 digests, updated parsing logic for both ASCII and binary logs, and expanded hashing support. Feedback identifies a critical bug in the binary encoding of v2 digests that would break hash verification and recommends improving error handling in the ASCII parser by propagating hex decoding errors instead of silently defaulting to empty values.

Comment thread src/log/template_hash.rs
Comment thread src/log/ascii.rs Outdated
Comment thread src/log/ascii.rs Outdated
@hyperfinitism hyperfinitism force-pushed the codex/refactor-ima-log-structure-to-use-enums branch from e6ad235 to 0dcc316 Compare April 29, 2026 15:52
@hyperfinitism hyperfinitism force-pushed the codex/refactor-ima-log-structure-to-use-enums branch from 0dcc316 to 8891a30 Compare April 29, 2026 16:23
Replace the stringly-typed template name with a typed Template enum and
add full parse/encode/decode coverage for every template documented in
the IMA spec. Drops the IMA_*_TEMPLATE_NAME string constants in favor of
Template::parse / Template::as_str.

New types:
- Template — enum with variants for ima, ima-ng, ima-sig, ima-buf,
  ima-modsig, ima-ngv2, ima-sigv2, evm-sig, plus Other(String) for
  forward compatibility.
- DigestType (Ima / Verity / Other) and DigestV2 — model the
  <digest_type>:<algo>:\0<digest> shape used by d-ngv2.
- TemplateData::{ImaModsig, ImaNgV2, ImaSigV2, EvmSig} variants and
  matching ImaModsigEntry / ImaNgV2Entry / ImaSigV2Entry / EvmSigEntry
  payload structs.

Parsers / encoders:
- Binary parser dispatches on Template, decoding the four new payload
  shapes (decode_ima_modsig/_ngv2/_sigv2/_evm_sig) plus the d-ngv2
  field decoder and u16/u32 LE field helpers.
- ASCII parser learns the same templates, including digest-type-prefixed
  d-ngv2 columns and the evm-sig column layout.
- template_hash framing covers the new TemplateData variants so
  recomputed template hashes still match the wire bytes.

Tests:
- Expanded src/log/parser.rs unit tests from 4 to 64 cases. New
  coverage: Endianness conversion, ParseOptions builders/accessors,
  decode_d_ng / decode_d_ngv2 / decode_n_ng success and error paths,
  parse_u32_le / parse_u16_le, split_fields edge cases,
  decode_legacy_ima error paths, per-template field-count rejection,
  end-to-end roundtrips through EventLogParser for every built-in
  template plus the Unknown fallback, and iterator semantics
  (empty input, mid-record EOF, fused-after-error, big-endian length
  decoding, max_field_len cap).
- Updated tests/integration.rs and tests/testdata.rs for the
  Event::template_name → Event::template rename.

Public API: Event::template_name (String) → Event::template (Template).
Examples and README adjusted accordingly.

Co-authored-by: chatgpt-codex-connector[bot] <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com>
@hyperfinitism hyperfinitism force-pushed the codex/refactor-ima-log-structure-to-use-enums branch from 8891a30 to a1547f9 Compare April 29, 2026 16:31
@hyperfinitism hyperfinitism marked this pull request as ready for review April 29, 2026 16:32
@hyperfinitism hyperfinitism merged commit cc65954 into main Apr 29, 2026
53 checks passed
@hyperfinitism hyperfinitism deleted the codex/refactor-ima-log-structure-to-use-enums branch April 29, 2026 16:36
Copy link
Copy Markdown
Contributor

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a1547f9d14

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/log/ascii.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant